//==============================================================================
#include "SoundBank.h"
#include <math.h>
//==============================================================================
CSoundBank CSoundBank::SoundControl;

//==============================================================================
CSoundBank::CSoundBank() {
	Mix_AllocateChannels(40);
}

//------------------------------------------------------------------------------
int CSoundBank::OnLoad(char* File, int soundType) {
    Mix_Chunk* TempSound = NULL;

    if((TempSound = Mix_LoadWAV(File)) == NULL) {
        return -1;
    }

	switch(soundType)
	{
	case MUSIC: MusicList.push_back(TempSound);
		break;
	case EFFECT: SoundList.push_back(TempSound);
		break;
	}
    return (SoundList.size() - 1);
}

//------------------------------------------------------------------------------
void CSoundBank::OnCleanup() {
    for(int i = 0;i < SoundList.size();i++) {
        Mix_FreeChunk(SoundList[i]);
    }
	SoundList.clear();

	for(int i = 0;i < MusicList.size();i++) {
        Mix_FreeChunk(MusicList[i]);
    }
    MusicList.clear();

}

//==============================================================================
void CSoundBank::Play(int ID, int soundType) {

	switch(soundType)
	{
	case EFFECT:
		if(ID < 0 || ID >= SoundList.size()) return;
		if(SoundList[ID] == NULL) return;
		
		Mix_PlayChannel(channelIncrementor, SoundList[ID], 0);
		++channelIncrementor;
		if(channelIncrementor == 8) channelIncrementor = 2;
		break;
	case MUSIC:
		if(ID < 0 || ID >= MusicList.size()) return;
		if(MusicList[ID] == NULL) return;

		Mix_PlayChannel(1, MusicList[ID], 0);
		break;
	}
}

void CSoundBank::Stop()
{
	 Mix_FadeOutChannel(1, 1000);
}

bool CSoundBank::IsPlaying()
{
	int i = Mix_Playing(1);
	if(i == 0)
		return false;
	else
		return true;
}

int CSoundBank::GetVolume(int soundType)
{
	switch(soundType)
	{
	case MUSIC:
		return Mix_Volume(1,-1);
	case EFFECT:
		return Mix_Volume(2,-1);
	default:
		return Mix_Volume(-1,-1);
	}
}

void CSoundBank::SetVolume(int volume, int soundType)
{
	switch(soundType)
	{
	case MUSIC:
		Mix_Volume(1, volume);
		break;
	case EFFECT:
		for(int i = 2; i <= 8; i++)
		{
			Mix_Volume(i, volume);
			break;
		}
	default:
		Mix_Volume(-1, volume);
		break;
	}
}

void CSoundBank::setSoundPosition(int x1, int y1, int x2, int y2)
{
	int distanceX = 0;
	int distanceY = 0;
	int distance = 0;
	float angle = 0;
	if(x1 <= x2 && y1 <= y2)
	{
		distanceX = x2 - x1;
		distanceY = y2 - y1;
	}
	else if(x1 >= x2 && y1 >= y2)
	{
		distanceX = x1 - x2;
		distanceY = y1 - y2;
	}
	else if(x1 <= x2 && y1 >= y2)
	{
		distanceX = x2 - x1;
		distanceY = y1 - y2;
	}
	else if(x1 >= x2 && y1 <= y2)
	{
		distanceX = x1 - x2;
		distanceY = y2 - y1;
	}
	
	distance = (int)sqrt((float)((distanceX*distanceX) + (distanceY*distanceY)));
		if(distance > 1000)
			distance = 255;
		else
		{
			distance = distance*100/255;
			if(distance > 255)
				distance = 255;
			if(distanceX != 0 && distanceY != 0)
			angle = tan((float)(distanceX/distanceY));
			if(angle < 10)
				angle *= 100;
		}
	Mix_SetPosition(channelIncrementor, angle, distance);
}


//==============================================================================
